package com.eliza.navs.fg

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import com.eliza.navs.R

class Fg4 : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_fg4, container, false)

        view.findViewById<View>(R.id.fg4_btn).setOnClickListener {
            Navigation.findNavController(view).navigate(R.id.action_fg4_to_fg5)
        }

        /**
         * 获取 itemId 数据
         * 生成的类名称会在源目的地类名称的基础上添加“Directions”
         * */
        val args: Fg3Args by navArgs() /*属性委托，简化从 bundle 获取数据的过程*/
        Log.d(this::class.java.name, "by navArgs::Fg4 <== Fg3:${args.userName}::${args.userAge}")
        fromBundle()
        argumentsJust()

        return view
    }

    private fun fromBundle() {
        arguments?.let {
            Fg3Args.fromBundle(it).apply {
                Log.d(
                    this::class.java.name,
                    "by navArgs::Fg4 <== Fg3:${this.userName}::${this.userAge}"
                )
            }
        }
    }

    private fun argumentsJust() {
        Log.d(
            this::class.java.name,
            "Bundle:Fg4 <== Fg :${arguments?.getString("userName")}::${arguments?.getString("userAge")}}"
        )
    }

}